﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Consts;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class QuerySProductByPlan : BasePage
    {
        int m_count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData(0);
            }
        }
        private void BindData(int pageIndex)
        {
            string userid = "";
            if (!string.IsNullOrEmpty(CurrentUser.UserID) && CurrentUser.UserID != CommonConsts.ADMIN_USER)
            {
                userid = CurrentUser.UserID;
            }
            DataTable dt = BsPlanBusiness.SelectDialogList(txtProductID.Text.Trim(), txtProductName.Text.Trim(), userid, pageIndex, pcData.PageSize, out m_count);
            pcData.RecordCount = m_count;
            if (dt.Rows.Count != 0)
            {
                grdResult.DataSource = dt;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData(0);
        }

        protected void grdResult_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                RadioButton rb = (RadioButton)e.Row.FindControl("rbSelect");
                rb.Attributes.Add("onclick", "judge(this)");
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string sReturn = "";
            foreach (GridViewRow gvr in grdResult.Rows)
            {
                Label lblBOMID = gvr.FindControl("lblBOMID") as Label;
                Label lblWorkOrderNo = gvr.FindControl("lblWorkOrderNo") as Label;
                Label lblProductID = gvr.FindControl("lblProductID") as Label;
                Label lblProductName = gvr.FindControl("lblProductName") as Label;
                Label lblCustomerPartNo = gvr.FindControl("lblCustomerPartNo") as Label;
                Label lblSpecifications = gvr.FindControl("lblSpecifications") as Label;
                Label lblUnit = gvr.FindControl("lblUnit") as Label;
                Label lblOutSite = gvr.FindControl("lblOutSite") as Label;
                Label lblInSite = gvr.FindControl("lblInSite") as Label;
                Label lblNums = gvr.FindControl("lblNums") as Label;
                Label lblLastNum = gvr.FindControl("lblLastNum") as Label;
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    if (((RadioButton)gvr.FindControl("rbSelect")).Checked)
                    {
                        sReturn = HttpUtility.UrlEncode(
                          lblWorkOrderNo.Text.Trim() + "," +
                          lblProductID.Text.Trim() + "," +
                          lblProductName.Text.Trim() + "," +
                          lblUnit.Text.Trim() + "," +
                          lblSpecifications.Text.Trim() + "," +
                          lblCustomerPartNo.Text.Trim() + "," +
                          lblNums.Text.Trim() + "," +
                          lblLastNum.Text.Trim() + "," +
                          lblInSite.Text.Trim() + "," +
                          lblOutSite.Text.Trim() + "," +
                          lblBOMID.Text.Trim() , Encoding.UTF8);
                        break;
                    }
                }
            }

            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + sReturn + "');CloseTopDialog();</script>");
        }

        protected void pcData_PageIndexClick(object sender, Controls.PageIndexClickEventArgs e)
        {
            BindData(e.PageIndex);
        }
    }
}